package run.siyuan.common.exception;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @program: sy-auth-manag
 * @description:
 * @author: SY
 * @create: 2021-01-13 20:40
 **/
@Slf4j
@Component
public class ExceptionProperties {
    private Properties exceptionProp;

    public ExceptionProperties() {
        log.info("===================ExceptionProperties=============");
    }

    @PostConstruct
    public void init() {
        this.exceptionProp = new Properties();
        InputStream is = null;
        Object bf = null;

        try {
            log.info("===================PostConstruct loading exception properties=============");
            this.load((InputStream)is, (BufferedReader)bf, "sy-exception.properties");
        } catch (Exception var12) {
            var12.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    ((InputStream)is).close();
                }

                if (bf != null) {
                    ((BufferedReader)bf).close();
                }
            } catch (IOException var11) {
                var11.printStackTrace();
            }

        }

        log.info("=================PostConstruct loaded exception.properties complite=============");
    }

    private void load(InputStream is, BufferedReader bf, String file) throws IOException {
        is = new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(file));
        bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        this.exceptionProp.load(bf);
        log.info("===================PostConstruct loaded " + file + "=============");
    }

    public Properties getExceptionProp() {
        return this.exceptionProp;
    }

    public String getCodeName(String code) {
        return this.exceptionProp.getProperty(code, this.exceptionProp.getProperty("00000"));
    }
}
